   /*******************************************************/
   /*      "C" Language Integrated Production System      */
   /*                                                     */
   /*               CLIPS Version 6.24  06/02/06          */
   /*                                                     */
   /*               CLASS INITIALIZATION MODULE           */
   /*******************************************************/

/**************************************************************/
/* Purpose: Defclass Initialization Routines                  */
/*                                                            */
/* Principal Programmer(s):                                   */
/*      Brian L. Dantes                                       */
/*                                                            */
/* Contributing Programmer(s):                                */
/*                                                            */
/* Revision History:                                          */
/*      6.23: Corrected compilation errors for files          */
/*            generated by constructs-to-c. DR0861            */
/*                                                            */
/*      6.24: Added allowed-classes slot facet.               */
/*                                                            */
/*            Converted INSTANCE_PATTERN_MATCHING to          */
/*            DEFRULE_CONSTRUCT.                              */
/*                                                            */
/*            Corrected code to remove run-time program       */
/*            compiler warning.                               */
/*                                                            */
/**************************************************************/

/* =========================================
   *****************************************
               EXTERNAL DEFINITIONS
   =========================================
   ***************************************** */
#include "setup.h"

#if OBJECT_SYSTEM

#ifndef _STDIO_INCLUDED_
#define _STDIO_INCLUDED_
#include <stdio.h>
#endif

#include "classcom.h"
#include "classexm.h"
#include "classfun.h"
#include "classinf.h"
#include "classpsr.h"
#include "cstrccom.h"
#include "cstrcpsr.h"
#include "envrnmnt.h"
#include "extnfunc.h"
#include "inscom.h"
#include "memalloc.h"
#include "modulpsr.h"
#include "modulutl.h"
#include "msgcom.h"
#include "watch.h"

#if DEFINSTANCES_CONSTRUCT
#include "defins.h"
#endif

#if INSTANCE_SET_QUERIES
#include "insquery.h"
#endif

#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
#include "bload.h"
#include "objbin.h"
#endif

#if CONSTRUCT_COMPILER && (! RUN_TIME)
#include "objcmp.h"
#endif

#if DEFRULE_CONSTRUCT
#include "objrtbld.h"
#include "objrtfnx.h"
#include "objrtmch.h"
#endif

#if RUN_TIME
#include "insfun.h"
#include "msgfun.h"
#endif

#include "classini.h"

/* =========================================
   *****************************************
                   CONSTANTS
   =========================================
   ***************************************** */
#define SUPERCLASS_RLN       "is-a"
#define NAME_RLN             "name"
#define INITIAL_OBJECT_NAME  "initial-object"

/* =========================================
   *****************************************
      INTERNALLY VISIBLE FUNCTION HEADERS
   =========================================
   ***************************************** */

static void SetupDefclasses(void *,EXEC_STATUS);
static void DeallocateDefclassData(void *,EXEC_STATUS);

#if (! RUN_TIME)
static void DestroyDefclassAction(void *,EXEC_STATUS,struct constructHeader *,void *);
static DEFCLASS *AddSystemClass(void *,EXEC_STATUS,char *,DEFCLASS *);
static void *AllocateModule(void *,EXEC_STATUS);
static void  ReturnModule(void *,EXEC_STATUS,void *);
#else
static void SearchForHashedPatternNodes(void *,EXEC_STATUS,OBJECT_PATTERN_NODE *);
#endif

#if (! BLOAD_ONLY) && (! RUN_TIME) && DEFMODULE_CONSTRUCT
static void UpdateDefclassesScope(void *,EXEC_STATUS);
#endif

/* =========================================
   *****************************************
          EXTERNALLY VISIBLE FUNCTIONS
   =========================================
   ***************************************** */

/**********************************************************
  NAME         : SetupObjectSystem
  DESCRIPTION  : Initializes all COOL constructs, functions,
                   and data structures
  INPUTS       : None
  RETURNS      : Nothing useful
  SIDE EFFECTS : COOL initialized
  NOTES        : Order of setup calls is important
 **********************************************************/
globle void SetupObjectSystem(
  void *theEnv,
  EXEC_STATUS)
  {   
   ENTITY_RECORD defclassEntityRecord = { "DEFCLASS_PTR", DEFCLASS_PTR,1,0,0,
                                              NULL,NULL,NULL,NULL,NULL,
                                              DecrementDefclassBusyCount,
                                              IncrementDefclassBusyCount,
                                              NULL,NULL,NULL,NULL,NULL };

   AllocateEnvironmentData(theEnv,execStatus,DEFCLASS_DATA,sizeof(struct defclassData),NULL);
   AddEnvironmentCleanupFunction(theEnv,execStatus,"defclasses",DeallocateDefclassData,-500);

   memcpy(&DefclassData(theEnv,execStatus)->DefclassEntityRecord,&defclassEntityRecord,sizeof(struct entityRecord));   

#if ! RUN_TIME
   DefclassData(theEnv,execStatus)->ClassDefaultsMode = CONVENIENCE_MODE;
   DefclassData(theEnv,execStatus)->ISA_SYMBOL = (SYMBOL_HN *) EnvAddSymbol(theEnv,execStatus,SUPERCLASS_RLN);
   IncrementSymbolCount(DefclassData(theEnv,execStatus)->ISA_SYMBOL);
   DefclassData(theEnv,execStatus)->NAME_SYMBOL = (SYMBOL_HN *) EnvAddSymbol(theEnv,execStatus,NAME_RLN);
   IncrementSymbolCount(DefclassData(theEnv,execStatus)->NAME_SYMBOL);
#if DEFRULE_CONSTRUCT
   DefclassData(theEnv,execStatus)->INITIAL_OBJECT_SYMBOL = (SYMBOL_HN *) EnvAddSymbol(theEnv,execStatus,INITIAL_OBJECT_NAME);
   IncrementSymbolCount(DefclassData(theEnv,execStatus)->INITIAL_OBJECT_SYMBOL);
#endif
#endif

   SetupDefclasses(theEnv,execStatus);
   SetupInstances(theEnv,execStatus);
   SetupMessageHandlers(theEnv,execStatus);

#if DEFINSTANCES_CONSTRUCT
   SetupDefinstances(theEnv,execStatus);
#endif

#if INSTANCE_SET_QUERIES
   SetupQuery(theEnv,execStatus);
#endif

#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
   SetupObjectsBload(theEnv,execStatus);
#endif

#if CONSTRUCT_COMPILER && (! RUN_TIME)
   SetupObjectsCompiler(theEnv,execStatus);
#endif

#if DEFRULE_CONSTRUCT
   SetupObjectPatternStuff(theEnv,execStatus);
#endif
  }
  
/***************************************************/
/* DeallocateDefclassData: Deallocates environment */
/*    data for the defclass construct.             */
/***************************************************/
static void DeallocateDefclassData(
  void *theEnv,
  EXEC_STATUS)
  {
#if ! RUN_TIME   
   SLOT_NAME *tmpSNPPtr, *nextSNPPtr;
   int i;
   struct defclassModule *theModuleItem;
   void *theModule;
   int bloaded = FALSE;
   
#if BLOAD || BLOAD_AND_BSAVE
   if (Bloaded(theEnv,execStatus)) bloaded = TRUE;
#endif

   /*=============================*/
   /* Destroy all the defclasses. */
   /*=============================*/
   
   if (! bloaded)
     {
      DoForAllConstructs(theEnv,execStatus,DestroyDefclassAction,DefclassData(theEnv,execStatus)->DefclassModuleIndex,FALSE,NULL); 

      for (theModule = EnvGetNextDefmodule(theEnv,execStatus,NULL);
           theModule != NULL;
           theModule = EnvGetNextDefmodule(theEnv,execStatus,theModule))
        {
         theModuleItem = (struct defclassModule *)
                         GetModuleItem(theEnv,execStatus,(struct defmodule *) theModule,
                                       DefclassData(theEnv,execStatus)->DefclassModuleIndex);
         rtn_struct(theEnv,execStatus,defclassModule,theModuleItem);
        }
     }

   /*==========================*/
   /* Remove the class tables. */
   /*==========================*/
   
   if (! bloaded)
     {
      if (DefclassData(theEnv,execStatus)->ClassIDMap != NULL)
        {
         genfree(theEnv,execStatus,DefclassData(theEnv,execStatus)->ClassIDMap,DefclassData(theEnv,execStatus)->AvailClassID * sizeof(DEFCLASS *));
        }
     }
     
   if (DefclassData(theEnv,execStatus)->ClassTable != NULL)
     {
      genfree(theEnv,execStatus,DefclassData(theEnv,execStatus)->ClassTable,sizeof(DEFCLASS *) * CLASS_TABLE_HASH_SIZE);
     }

   /*==============================*/
   /* Free up the slot name table. */
   /*==============================*/

   if (! bloaded)
     {
      for (i = 0; i < SLOT_NAME_TABLE_HASH_SIZE; i++)
        {
         tmpSNPPtr = DefclassData(theEnv,execStatus)->SlotNameTable[i];
      
         while (tmpSNPPtr != NULL)
           {
            nextSNPPtr = tmpSNPPtr->nxt;
            rtn_struct(theEnv,execStatus,slotName,tmpSNPPtr);
            tmpSNPPtr = nextSNPPtr;
           }
        }
     }
          
   if (DefclassData(theEnv,execStatus)->SlotNameTable != NULL)
     {
      genfree(theEnv,execStatus,DefclassData(theEnv,execStatus)->SlotNameTable,sizeof(SLOT_NAME *) * SLOT_NAME_TABLE_HASH_SIZE);
     }
#else
   DEFCLASS *cls;
   void *tmpexp;
   register unsigned int i;
   register int j;
   
   if (DefclassData(theEnv,execStatus)->ClassTable != NULL)
     {
      for (j = 0 ; j < CLASS_TABLE_HASH_SIZE ; j++)
        for (cls = DefclassData(theEnv,execStatus)->ClassTable[j] ; cls != NULL ; cls = cls->nxtHash)
          {
           for (i = 0 ; i < cls->slotCount ; i++)
             {
              if ((cls->slots[i].defaultValue != NULL) && (cls->slots[i].dynamicDefault == 0))
                {
                 tmpexp = ((DATA_OBJECT *) cls->slots[i].defaultValue)->supplementalInfo;
                 rtn_struct(theEnv,execStatus,dataObject,cls->slots[i].defaultValue);
                 cls->slots[i].defaultValue = tmpexp;
                }
             }
          }
     }
#endif
  }
  
#if ! RUN_TIME
/*********************************************************/
/* DestroyDefclassAction: Action used to remove defclass */
/*   as a result of DestroyEnvironment.                  */
/*********************************************************/
#if WIN_BTC
#pragma argsused
#endif
static void DestroyDefclassAction(
  void *theEnv,
  EXEC_STATUS,
  struct constructHeader *theConstruct,
  void *buffer)
  {
#if MAC_MCW || WIN_MCW || MAC_XCD
#pragma unused(buffer)
#endif
   struct defclass *theDefclass = (struct defclass *) theConstruct;

   if (theDefclass == NULL) return;

#if (! BLOAD_ONLY) 
   DestroyDefclass(theEnv,execStatus,theDefclass);
#else
#if MAC_MCW || WIN_MCW || MAC_XCD
#pragma unused(theEnv,execStatus)
#endif
#endif
  }
#endif

#if RUN_TIME

/***************************************************
  NAME         : ObjectsRunTimeInitialize
  DESCRIPTION  : Initializes objects system lists
                   in a run-time module
  INPUTS       : 1) Pointer to new class hash table
                 2) Pointer to new slot name table
  RETURNS      : Nothing useful
  SIDE EFFECTS : Global pointers set
  NOTES        : None
 ***************************************************/
globle void ObjectsRunTimeInitialize(
  void *theEnv,
  EXEC_STATUS,
  DEFCLASS *ctable[],
  SLOT_NAME *sntable[],
  DEFCLASS **cidmap,
  unsigned mid)
  {
   DEFCLASS *cls;
   void *tmpexp;
   register unsigned int i,j;

   if (DefclassData(theEnv,execStatus)->ClassTable != NULL)
     {
      for (j = 0 ; j < CLASS_TABLE_HASH_SIZE ; j++)
        for (cls = DefclassData(theEnv,execStatus)->ClassTable[j] ; cls != NULL ; cls = cls->nxtHash)
          {
           for (i = 0 ; i < cls->slotCount ; i++)
             {
              /* =====================================================================
                 For static default values, the data object value needs to deinstalled
                 and deallocated, and the expression needs to be restored (which was
                 temporarily stored in the supplementalInfo field of the data object)
                 ===================================================================== */
              if ((cls->slots[i].defaultValue != NULL) && (cls->slots[i].dynamicDefault == 0))
                {
                 tmpexp = ((DATA_OBJECT *) cls->slots[i].defaultValue)->supplementalInfo;
                 ValueDeinstall(theEnv,execStatus,(DATA_OBJECT *) cls->slots[i].defaultValue);
                 rtn_struct(theEnv,execStatus,dataObject,cls->slots[i].defaultValue);
                 cls->slots[i].defaultValue = tmpexp;
                }
             }
          }
     }

   InstanceQueryData(theEnv,execStatus)->QUERY_DELIMETER_SYMBOL = FindSymbolHN(theEnv,execStatus,QUERY_DELIMETER_STRING);
   MessageHandlerData(theEnv,execStatus)->INIT_SYMBOL = FindSymbolHN(theEnv,execStatus,INIT_STRING);
   MessageHandlerData(theEnv,execStatus)->DELETE_SYMBOL = FindSymbolHN(theEnv,execStatus,DELETE_STRING);
   MessageHandlerData(theEnv,execStatus)->CREATE_SYMBOL = FindSymbolHN(theEnv,execStatus,CREATE_STRING);
   DefclassData(theEnv,execStatus)->ISA_SYMBOL = FindSymbolHN(theEnv,execStatus,SUPERCLASS_RLN);
   DefclassData(theEnv,execStatus)->NAME_SYMBOL = FindSymbolHN(theEnv,execStatus,NAME_RLN);
#if DEFRULE_CONSTRUCT
   DefclassData(theEnv,execStatus)->INITIAL_OBJECT_SYMBOL = FindSymbolHN(theEnv,execStatus,INITIAL_OBJECT_NAME);
#endif

   DefclassData(theEnv,execStatus)->ClassTable = (DEFCLASS **) ctable;
   DefclassData(theEnv,execStatus)->SlotNameTable = (SLOT_NAME **) sntable;
   DefclassData(theEnv,execStatus)->ClassIDMap = (DEFCLASS **) cidmap;
   DefclassData(theEnv,execStatus)->MaxClassID = (unsigned short) mid;
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[FLOAT] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,FLOAT_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[INTEGER] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,INTEGER_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[STRING] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,STRING_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[SYMBOL] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,SYMBOL_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[MULTIFIELD] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,MULTIFIELD_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[EXTERNAL_ADDRESS] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,EXTERNAL_ADDRESS_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[FACT_ADDRESS] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,FACT_ADDRESS_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_NAME] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,INSTANCE_NAME_TYPE_NAME);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_ADDRESS] =
     LookupDefclassByMdlOrScope(theEnv,execStatus,INSTANCE_ADDRESS_TYPE_NAME);

   for (j = 0 ; j < CLASS_TABLE_HASH_SIZE ; j++)
     for (cls = DefclassData(theEnv,execStatus)->ClassTable[j] ; cls != NULL ; cls = cls->nxtHash)
     {
      for (i = 0 ; i < cls->slotCount ; i++)
        {
         if ((cls->slots[i].defaultValue != NULL) && (cls->slots[i].dynamicDefault == 0))
           {
            tmpexp = cls->slots[i].defaultValue;
            cls->slots[i].defaultValue = (void *) get_struct(theEnv,execStatus,dataObject);
            EvaluateAndStoreInDataObject(theEnv,execStatus,(int) cls->slots[i].multiple,(EXPRESSION *) tmpexp,
                                         (DATA_OBJECT *) cls->slots[i].defaultValue,TRUE);
            ValueInstall(theEnv,execStatus,(DATA_OBJECT *) cls->slots[i].defaultValue);
            ((DATA_OBJECT *) cls->slots[i].defaultValue)->supplementalInfo = tmpexp;
           }
        }
     }
     
   SearchForHashedPatternNodes(theEnv,execStatus,ObjectReteData(theEnv,execStatus)->ObjectPatternNetworkPointer);
  }
  
/*******************************************************************/
/* SearchForHashedPatternNodes:    */
/*******************************************************************/
static void SearchForHashedPatternNodes(
   void *theEnv,
  EXEC_STATUS,
   OBJECT_PATTERN_NODE *theNode)
   {
    while (theNode != NULL)
      {
       if ((theNode->lastLevel != NULL) && (theNode->lastLevel->selector))
        { AddHashedPatternNode(theEnv,execStatus,theNode->lastLevel,theNode,theNode->networkTest->type,theNode->networkTest->value); }

       SearchForHashedPatternNodes(theEnv,execStatus,theNode->nextLevel);
      
       theNode = theNode->rightNode;
      }
   }

#else

/***************************************************************
  NAME         : CreateSystemClasses
  DESCRIPTION  : Creates the built-in system classes
  INPUTS       : None
  RETURNS      : Nothing useful
  SIDE EFFECTS : System classes inserted in the
                   class hash table
  NOTES        : The binary/load save indices for the primitive
                   types (integer, float, symbol and string,
                   multifield, external-address and fact-address)
                   are very important.  Need to be able to refer
                   to types with the same index regardless of
                   whether the object system is installed or
                   not.  Thus, the bsave/blaod indices of these
                   classes match their integer codes.
                WARNING!!: Assumes no classes exist yet!
 ***************************************************************/
globle void CreateSystemClasses(
  void *theEnv,
  EXEC_STATUS)
  {
   DEFCLASS *user,*any,*primitive,*number,*lexeme,*address,*instance;
#if DEFRULE_CONSTRUCT
   DEFCLASS *initialObject;
#endif
   
   /* ===================================
      Add canonical slot name entries for
      the is-a and name fields - used for
      object patterns
      =================================== */
   AddSlotName(theEnv,execStatus,DefclassData(theEnv,execStatus)->ISA_SYMBOL,ISA_ID,TRUE);
   AddSlotName(theEnv,execStatus,DefclassData(theEnv,execStatus)->NAME_SYMBOL,NAME_ID,TRUE);

   /* =========================================================
      Bsave Indices for non-primitive classes start at 9
               Object is 9, Primitive is 10, Number is 11,
               Lexeme is 12, Address is 13, and Instance is 14.
      because: float = 0, integer = 1, symbol = 2, string = 3,
               multifield = 4, and external-address = 5 and
               fact-address = 6, instance-adress = 7 and
               instance-name = 8.
      ========================================================= */
   any = AddSystemClass(theEnv,execStatus,OBJECT_TYPE_NAME,NULL);
   primitive = AddSystemClass(theEnv,execStatus,PRIMITIVE_TYPE_NAME,any);
   user = AddSystemClass(theEnv,execStatus,USER_TYPE_NAME,any);

   number = AddSystemClass(theEnv,execStatus,NUMBER_TYPE_NAME,primitive);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[INTEGER] = AddSystemClass(theEnv,execStatus,INTEGER_TYPE_NAME,number);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[FLOAT] = AddSystemClass(theEnv,execStatus,FLOAT_TYPE_NAME,number);
   lexeme = AddSystemClass(theEnv,execStatus,LEXEME_TYPE_NAME,primitive);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[SYMBOL] = AddSystemClass(theEnv,execStatus,SYMBOL_TYPE_NAME,lexeme);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[STRING] = AddSystemClass(theEnv,execStatus,STRING_TYPE_NAME,lexeme);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[MULTIFIELD] = AddSystemClass(theEnv,execStatus,MULTIFIELD_TYPE_NAME,primitive);
   address = AddSystemClass(theEnv,execStatus,ADDRESS_TYPE_NAME,primitive);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[EXTERNAL_ADDRESS] = AddSystemClass(theEnv,execStatus,EXTERNAL_ADDRESS_TYPE_NAME,address);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[FACT_ADDRESS] = AddSystemClass(theEnv,execStatus,FACT_ADDRESS_TYPE_NAME,address);
   instance = AddSystemClass(theEnv,execStatus,INSTANCE_TYPE_NAME,primitive);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_ADDRESS] = AddSystemClass(theEnv,execStatus,INSTANCE_ADDRESS_TYPE_NAME,instance);
   DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_NAME] = AddSystemClass(theEnv,execStatus,INSTANCE_NAME_TYPE_NAME,instance);
#if DEFRULE_CONSTRUCT
   initialObject = AddSystemClass(theEnv,execStatus,INITIAL_OBJECT_CLASS_NAME,user);
   initialObject->abstract = 0;
   initialObject->reactive = 1;
#endif

   /* ================================================================================
       INSTANCE-ADDRESS is-a INSTANCE and ADDRESS.  The links between INSTANCE-ADDRESS
       and ADDRESS still need to be made.
       =============================================================================== */
   AddClassLink(theEnv,execStatus,&DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_ADDRESS]->directSuperclasses,address,-1);
   AddClassLink(theEnv,execStatus,&DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_ADDRESS]->allSuperclasses,address,2);
   AddClassLink(theEnv,execStatus,&address->directSubclasses,DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_ADDRESS],-1);

   /* =======================================================================
      The order of the class in the list MUST correspond to their type codes!
      See CONSTANT.H
      ======================================================================= */
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[FLOAT]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[INTEGER]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[SYMBOL]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[STRING]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[MULTIFIELD]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[EXTERNAL_ADDRESS]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[FACT_ADDRESS]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_ADDRESS]);
   AddConstructToModule((struct constructHeader *) DefclassData(theEnv,execStatus)->PrimitiveClassMap[INSTANCE_NAME]);
   AddConstructToModule((struct constructHeader *) any);
   AddConstructToModule((struct constructHeader *) primitive);
   AddConstructToModule((struct constructHeader *) number);
   AddConstructToModule((struct constructHeader *) lexeme);
   AddConstructToModule((struct constructHeader *) address);
   AddConstructToModule((struct constructHeader *) instance);
   AddConstructToModule((struct constructHeader *) user);
#if DEFRULE_CONSTRUCT
   AddConstructToModule((struct constructHeader *) initialObject);
#endif
   for (any = (DEFCLASS *) EnvGetNextDefclass(theEnv,execStatus,NULL) ;
        any != NULL ;
        any = (DEFCLASS *) EnvGetNextDefclass(theEnv,execStatus,(void *) any))
     AssignClassID(theEnv,execStatus,any);
  }

#endif

/* =========================================
   *****************************************
          INTERNALLY VISIBLE FUNCTIONS
   =========================================
   ***************************************** */

/*********************************************************
  NAME         : SetupDefclasses
  DESCRIPTION  : Initializes Class Hash Table,
                   Function Parsers, and Data Structures
  INPUTS       : None
  RETURNS      : Nothing useful
  SIDE EFFECTS :
  NOTES        : None
 *********************************************************/
static void SetupDefclasses(
  void *theEnv,
  EXEC_STATUS)
  {
   InstallPrimitive(theEnv,execStatus,&DefclassData(theEnv,execStatus)->DefclassEntityRecord,DEFCLASS_PTR);

   DefclassData(theEnv,execStatus)->DefclassModuleIndex =
                RegisterModuleItem(theEnv,execStatus,"defclass",
#if (! RUN_TIME)
                                    AllocateModule,ReturnModule,
#else
                                    NULL,NULL,
#endif
#if BLOAD_AND_BSAVE || BLOAD || BLOAD_ONLY
                                    BloadDefclassModuleReference,
#else
                                    NULL,
#endif
#if CONSTRUCT_COMPILER && (! RUN_TIME)
                                    DefclassCModuleReference,
#else
                                    NULL,
#endif
                                    EnvFindDefclass);

   DefclassData(theEnv,execStatus)->DefclassConstruct =  AddConstruct(theEnv,execStatus,"defclass","defclasses",
#if (! BLOAD_ONLY) && (! RUN_TIME)
                                     ParseDefclass,
#else
                                     NULL,
#endif
                                     EnvFindDefclass,
                                     GetConstructNamePointer,GetConstructPPForm,
                                     GetConstructModuleItem,EnvGetNextDefclass,
                                     SetNextConstruct,EnvIsDefclassDeletable,
                                     EnvUndefclass,
#if (! RUN_TIME)
                                     RemoveDefclass
#else
                                     NULL
#endif
                                     );

   AddClearReadyFunction(theEnv,execStatus,"defclass",InstancesPurge,0);

#if ! RUN_TIME
   EnvAddClearFunction(theEnv,execStatus,"defclass",CreateSystemClasses,0);
   InitializeClasses(theEnv,execStatus);

#if ! BLOAD_ONLY
#if DEFMODULE_CONSTRUCT
   AddPortConstructItem(theEnv,execStatus,"defclass",SYMBOL);
   AddAfterModuleDefinedFunction(theEnv,execStatus,"defclass",UpdateDefclassesScope,0);
#endif
   EnvDefineFunction2(theEnv,execStatus,"undefclass",'v',PTIEF UndefclassCommand,"UndefclassCommand","11w");

   AddSaveFunction(theEnv,execStatus,"defclass",SaveDefclasses,10);
#endif

#if DEBUGGING_FUNCTIONS
   EnvDefineFunction2(theEnv,execStatus,"list-defclasses",'v',PTIEF ListDefclassesCommand,"ListDefclassesCommand","01");
   EnvDefineFunction2(theEnv,execStatus,"ppdefclass",'v',PTIEF PPDefclassCommand,"PPDefclassCommand","11w");
   EnvDefineFunction2(theEnv,execStatus,"describe-class",'v',PTIEF DescribeClassCommand,"DescribeClassCommand","11w");
   EnvDefineFunction2(theEnv,execStatus,"browse-classes",'v',PTIEF BrowseClassesCommand,"BrowseClassesCommand","01w");
#endif

   EnvDefineFunction2(theEnv,execStatus,"get-defclass-list",'m',PTIEF GetDefclassListFunction,
                   "GetDefclassListFunction","01");
   EnvDefineFunction2(theEnv,execStatus,"superclassp",'b',PTIEF SuperclassPCommand,"SuperclassPCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"subclassp",'b',PTIEF SubclassPCommand,"SubclassPCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"class-existp",'b',PTIEF ClassExistPCommand,"ClassExistPCommand","11w");
   EnvDefineFunction2(theEnv,execStatus,"message-handler-existp",'b',
                   PTIEF MessageHandlerExistPCommand,"MessageHandlerExistPCommand","23w");
   EnvDefineFunction2(theEnv,execStatus,"class-abstractp",'b',PTIEF ClassAbstractPCommand,"ClassAbstractPCommand","11w");
#if DEFRULE_CONSTRUCT
   EnvDefineFunction2(theEnv,execStatus,"class-reactivep",'b',PTIEF ClassReactivePCommand,"ClassReactivePCommand","11w");
#endif
   EnvDefineFunction2(theEnv,execStatus,"class-slots",'m',PTIEF ClassSlotsCommand,"ClassSlotsCommand","12w");
   EnvDefineFunction2(theEnv,execStatus,"class-superclasses",'m',
                   PTIEF ClassSuperclassesCommand,"ClassSuperclassesCommand","12w");
   EnvDefineFunction2(theEnv,execStatus,"class-subclasses",'m',
                   PTIEF ClassSubclassesCommand,"ClassSubclassesCommand","12w");
   EnvDefineFunction2(theEnv,execStatus,"get-defmessage-handler-list",'m',
                   PTIEF GetDefmessageHandlersListCmd,"GetDefmessageHandlersListCmd","02w");
   EnvDefineFunction2(theEnv,execStatus,"slot-existp",'b',PTIEF SlotExistPCommand,"SlotExistPCommand","23w");
   EnvDefineFunction2(theEnv,execStatus,"slot-facets",'m',PTIEF SlotFacetsCommand,"SlotFacetsCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-sources",'m',PTIEF SlotSourcesCommand,"SlotSourcesCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-types",'m',PTIEF SlotTypesCommand,"SlotTypesCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-allowed-values",'m',PTIEF SlotAllowedValuesCommand,"SlotAllowedValuesCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-allowed-classes",'m',PTIEF SlotAllowedClassesCommand,"SlotAllowedClassesCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-range",'m',PTIEF SlotRangeCommand,"SlotRangeCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-cardinality",'m',PTIEF SlotCardinalityCommand,"SlotCardinalityCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-writablep",'b',PTIEF SlotWritablePCommand,"SlotWritablePCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-initablep",'b',PTIEF SlotInitablePCommand,"SlotInitablePCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-publicp",'b',PTIEF SlotPublicPCommand,"SlotPublicPCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-direct-accessp",'b',PTIEF SlotDirectAccessPCommand,
                   "SlotDirectAccessPCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"slot-default-value",'u',PTIEF SlotDefaultValueCommand,
                   "SlotDefaultValueCommand","22w");
   EnvDefineFunction2(theEnv,execStatus,"defclass-module",'w',PTIEF GetDefclassModuleCommand,
                   "GetDefclassModuleCommand","11w");
   EnvDefineFunction2(theEnv,execStatus,"get-class-defaults-mode", 'w', PTIEF GetClassDefaultsModeCommand,  "GetClassDefaultsModeCommand", "00");
   EnvDefineFunction2(theEnv,execStatus,"set-class-defaults-mode", 'w', PTIEF SetClassDefaultsModeCommand,  "SetClassDefaultsModeCommand", "11w");
#endif

#if DEBUGGING_FUNCTIONS
   AddWatchItem(theEnv,execStatus,"instances",0,&DefclassData(theEnv,execStatus)->WatchInstances,75,DefclassWatchAccess,DefclassWatchPrint);
   AddWatchItem(theEnv,execStatus,"slots",1,&DefclassData(theEnv,execStatus)->WatchSlots,74,DefclassWatchAccess,DefclassWatchPrint);
#endif
  }

#if (! RUN_TIME)

/*********************************************************
  NAME         : AddSystemClass
  DESCRIPTION  : Performs all necessary allocations
                   for adding a system class
  INPUTS       : 1) The name-string of the system class
                 2) The address of the parent class
                    (NULL if none)
  RETURNS      : The address of the new system class
  SIDE EFFECTS : Allocations performed
  NOTES        : Assumes system-class name is unique
                 Also assumes SINGLE INHERITANCE for
                   system classes to simplify precedence
                   list determination
                 Adds classes to has table but NOT to
                  class list (this is responsibility
                  of caller)
 *********************************************************/
static DEFCLASS *AddSystemClass(
  void *theEnv,
  EXEC_STATUS,
  char *name,
  DEFCLASS *parent)
  {
   DEFCLASS *sys;
   long i;
   char defaultScopeMap[1];

   sys = NewClass(theEnv,execStatus,(SYMBOL_HN *) EnvAddSymbol(theEnv,execStatus,name));
   sys->abstract = 1;
#if DEFRULE_CONSTRUCT
   sys->reactive = 0;
#endif
   IncrementSymbolCount(sys->header.name);
   sys->installed = 1;
   sys->system = 1;
   sys->hashTableIndex = HashClass(sys->header.name);

   AddClassLink(theEnv,execStatus,&sys->allSuperclasses,sys,-1);
   if (parent != NULL)
     {
      AddClassLink(theEnv,execStatus,&sys->directSuperclasses,parent,-1);
      AddClassLink(theEnv,execStatus,&parent->directSubclasses,sys,-1);
      AddClassLink(theEnv,execStatus,&sys->allSuperclasses,parent,-1);
      for (i = 1 ; i < parent->allSuperclasses.classCount ; i++)
        AddClassLink(theEnv,execStatus,&sys->allSuperclasses,parent->allSuperclasses.classArray[i],-1);
     }
   sys->nxtHash = DefclassData(theEnv,execStatus)->ClassTable[sys->hashTableIndex];
   DefclassData(theEnv,execStatus)->ClassTable[sys->hashTableIndex] = sys;

   /* =========================================
      Add default scope maps for a system class
      There is only one module (MAIN) so far -
      which has an id of 0
      ========================================= */
   ClearBitString((void *) defaultScopeMap,(int) sizeof(char));
   SetBitMap(defaultScopeMap,0);
#if DEFMODULE_CONSTRUCT
   sys->scopeMap = (BITMAP_HN *) EnvAddBitMap(theEnv,execStatus,(void *) defaultScopeMap,(int) sizeof(char));
   IncrementBitMapCount(sys->scopeMap);
#endif
   return(sys);
  }

/*****************************************************
  NAME         : AllocateModule
  DESCRIPTION  : Creates and initializes a
                 list of deffunctions for a new module
  INPUTS       : None
  RETURNS      : The new deffunction module
  SIDE EFFECTS : Deffunction module created
  NOTES        : None
 *****************************************************/
static void *AllocateModule(
  void *theEnv,
  EXEC_STATUS)
  {
   return((void *) get_struct(theEnv,execStatus,defclassModule));
  }

/***************************************************
  NAME         : ReturnModule
  DESCRIPTION  : Removes a deffunction module and
                 all associated deffunctions
  INPUTS       : The deffunction module
  RETURNS      : Nothing useful
  SIDE EFFECTS : Module and deffunctions deleted
  NOTES        : None
 ***************************************************/
static void ReturnModule(
  void *theEnv,
  EXEC_STATUS,
  void *theItem)
  {
   FreeConstructHeaderModule(theEnv,execStatus,(struct defmoduleItemHeader *) theItem,DefclassData(theEnv,execStatus)->DefclassConstruct);
   DeleteSlotName(theEnv,execStatus,FindIDSlotNameHash(theEnv,execStatus,ISA_ID));
   DeleteSlotName(theEnv,execStatus,FindIDSlotNameHash(theEnv,execStatus,NAME_ID));
   rtn_struct(theEnv,execStatus,defclassModule,theItem);
  }

#endif

#if (! BLOAD_ONLY) && (! RUN_TIME) && DEFMODULE_CONSTRUCT

/***************************************************
  NAME         : UpdateDefclassesScope
  DESCRIPTION  : This function updates the scope
                 bitmaps for existing classes when
                 a new module is defined
  INPUTS       : None
  RETURNS      : Nothing
  SIDE EFFECTS : Class scope bitmaps are updated
  NOTES        : None
 ***************************************************/
static void UpdateDefclassesScope(
  void *theEnv,
  EXEC_STATUS)
  {
   register unsigned i;
   DEFCLASS *theDefclass;
   int newModuleID,count;
   char *newScopeMap;
   unsigned newScopeMapSize;
   char *className;
   struct defmodule *matchModule;

   newModuleID = (int) ((struct defmodule *) EnvGetCurrentModule(theEnv,execStatus))->bsaveID;
   newScopeMapSize = (sizeof(char) * ((GetNumberOfDefmodules(theEnv,execStatus) / BITS_PER_BYTE) + 1));
   newScopeMap = (char *) gm2(theEnv,execStatus,newScopeMapSize);
   for (i = 0 ; i < CLASS_TABLE_HASH_SIZE ; i++)
     for (theDefclass = DefclassData(theEnv,execStatus)->ClassTable[i] ;
          theDefclass != NULL ;
          theDefclass = theDefclass->nxtHash)
       {
        matchModule = theDefclass->header.whichModule->theModule;
        className = ValueToString(theDefclass->header.name);
        ClearBitString((void *) newScopeMap,newScopeMapSize);
        GenCopyMemory(char,theDefclass->scopeMap->size,
                   newScopeMap,ValueToBitMap(theDefclass->scopeMap));
        DecrementBitMapCount(theEnv,execStatus,theDefclass->scopeMap);
        if (theDefclass->system)
          SetBitMap(newScopeMap,newModuleID);
        else if (FindImportedConstruct(theEnv,execStatus,"defclass",matchModule,
                                       className,&count,TRUE,NULL) != NULL)
          SetBitMap(newScopeMap,newModuleID);
        theDefclass->scopeMap = (BITMAP_HN *) EnvAddBitMap(theEnv,execStatus,(void *) newScopeMap,newScopeMapSize);
        IncrementBitMapCount(theDefclass->scopeMap);
       }
   rm(theEnv,execStatus,(void *) newScopeMap,newScopeMapSize);
  }

#endif

#endif
